//
//  VCOfThird.m
//  17--WeekendHomeWork
//
//  Created by 冀永金 on 16/8/6.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "VCOfThird.h"
#import "Contact.h"
#import "DataBaseHandler.h"
#import "ReadandSaveCaches.h"
@interface VCOfThird ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, copy) NSString *imagePath;
@end

@implementation VCOfThird

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    // Do any additional setup after loading the view.
}
- (void)config {
    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 150, 200)];
    [self.view addSubview:_image];
    _image.backgroundColor = [UIColor redColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle:)];
    [_image addGestureRecognizer:tap];
    _image.userInteractionEnabled = YES;
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    if ([users dataForKey:@"image"]) {
        [_image setImage:[UIImage imageWithData:[users dataForKey:@"image"]]];
    }
    
    
    
    UILabel *namelabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 5, 80, 40)];
    [self.view addSubview:namelabel];
    namelabel.text = @"姓名：";
    _textFieldName = [[UITextField alloc]initWithFrame:CGRectMake(245, 5, 115, 40)];
    [self.view addSubview:_textFieldName];
    _textFieldName.placeholder = @"请输入姓名";
    //_textFieldName.text = self.name;
    //namelabel.backgroundColor = [UIColor yellowColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(160, 55, 80, 40)];
    [self.view addSubview:label];
    label.text = @"性别：";
    self.genderText = [[UITextField alloc]initWithFrame:CGRectMake(245, 55, 115, 40)];
    [self.view addSubview:self.genderText];
    self.genderText.placeholder = @"请输入性别";
   // _genderText.text = self.genderContent;
    UILabel *phoneNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 100, 80, 40)];
    [self.view addSubview:phoneNumLabel];
    //phoneNumLabel.backgroundColor = [UIColor yellowColor];
    phoneNumLabel.text = @"电话号：";
    self.phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(245, 100, 115, 40)];
    [self.view addSubview:self.phoneTextField];
    self.phoneTextField.placeholder = @"请输入电话号";
   // _phoneTextField.text = self.phoneContent;
    UILabel *labelAdress = [[UILabel alloc]initWithFrame:CGRectMake(5, 210, 100, 40)];
    [self.view addSubview:labelAdress];
    labelAdress.text = @"家庭地址：";
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(110, 210, self.view.bounds.size.width - 10 - 80, 40)];
    [self.view addSubview:textView];
    textView.backgroundColor = [UIColor lightGrayColor];
    textView.font = [UIFont systemFontOfSize:20];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    button.frame = CGRectMake(5, 255, self.view.bounds.size.width - 10, 40);
    [button addTarget:self action:@selector(handleOK:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleOK:(UIButton *)button {
    Contact *con = [[Contact alloc]init];
    con.name = _textFieldName.text;
    con.phoneNumber = _phoneTextField.text;
    [[DataBaseHandler shareManager]insertContact:con];
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject] stringByAppendingPathComponent:@"path"];
    NSLog(@"%@",NSHomeDirectory());
    NSError *error = nil;
   [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];

    NSString *documents = [path stringByAppendingPathComponent:@"image"];
    
    NSLog(@"----------%@",documents);
    //拼接文件绝对路径
    //NSString *path1 = [documents stringByAppendingPathComponent:@"1"];
    NSData *imagedata = UIImageJPEGRepresentation(_image.image, 0.1);
    //保存
    [imagedata writeToFile:documents atomically:YES];
    
    
}
- (void)handle:(UITapGestureRecognizer *)tap {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    //保存编辑照片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (image != nil) {
        _image.image = image;
    }
    NSData *data;
    if (UIImagePNGRepresentation(image)) {
        data = UIImagePNGRepresentation(image);
    }
    else {
        data = UIImageJPEGRepresentation(image, 1.0);
    }
    [[NSFileManager defaultManager]createFileAtPath:_imagePath contents:data attributes:nil];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:data forKey:@"image"];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
